//
//  UIImage+hf.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/17.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "UIImage+hf.h"

@implementation UIImage (hf)

@end


@implementation UIImage (Color)

-(UIImage*)imageScale:(float)scaleSize {
    UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scaleSize, self.size.height * scaleSize));
    [self drawInRect:CGRectMake(0, 0, self.size.width * scaleSize, self.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


-(UIImage *)imageToSize:(CGSize)reSize {
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [self drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

-(instancetype)initWithView:(UIView *)theView {
    CGRect rect = theView.frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    self = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return self;
}
-(UIImage*)getImageFromImage{
    
    CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
    UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
    CGImageRef imageRef = bigImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
    CGSize size;
    size.width = 57.0;
    size.height = 57.0;
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, myImageRect, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    return smallImage;
}


//
//                                (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
//                                    UIGraphicsBeginImageContext(image1.size);
//                                    // Draw image1
//                                    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
//                                    // Draw image2
//                                    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
//                                    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
//                                    UIGraphicsEndImageContext();
//                                    return resultingImage;
//                                }

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpa:(CGFloat)alpa {
    // 获取图片大小
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    // 创建位图绘图上下文
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
    // 获取位图绘图上下文并开始渲染操作
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawInRect:rect];
    
    // 全局渲染成需要的颜色(会和图片进行混合, 只要设置成混合模式是kCGBlendModeSourceAtop, 就能得到图片轮廓但是图颜色是我们需要的颜色)
    CGContextSetRGBFillColor(context, red, green, blue, alpa);
    // 纯色图片渲染的关键设置
    CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
    // 全局渲染颜色
    CGContextFillRect(context, rect);
    // 获取到绘图结果，结束位图绘图上下文并返回绘图结果
    UIImage *resulet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resulet;
}

+ (UIImage *)imageWithView:(UIView *)view {
    CGSize s = view.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
